package com.ereport.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.ereport.entity.AdminExpensesToExec;
import com.ereport.entity.Expenses;
import com.ereport.util.ApplicationContext;

public class ExpensesInAdminDAO {
static Transaction tx;
	
	private  void endOperations(Session session) {
		
		session.close();
		session=null;
	}

	private  Session startOperations()throws Exception {
		
		Session session = ApplicationContext.getHiberSession().openSession();
		tx=session.beginTransaction();
		return session;
		
	}

	public AdminExpensesToExec getExpens(int exeid, String mnth, String yr) throws Exception {
		Session session = startOperations();
		String qrystr="";
		qrystr="From AdminExpensesToExec where exe_id=:id and expnsdate like '"+yr+"-"+mnth+"'";
		Query qr=session.createQuery(qrystr);
		qr.setInteger("id", exeid);
		AdminExpensesToExec ob=(AdminExpensesToExec)qr.uniqueResult();
		session.getTransaction().commit();
		endOperations(session);
		return ob;
	}

	public int insert(AdminExpensesToExec ae) throws Exception {
		Session session = startOperations();
		session.saveOrUpdate(ae);
		session.getTransaction().commit();
		endOperations(session);
		return 0;
		
	}

	public List<AdminExpensesToExec> getViewExpens() throws Exception {
		List<AdminExpensesToExec> ob=new ArrayList<AdminExpensesToExec>();
		Session session = startOperations();
		String qrystr="From AdminExpensesToExec order by expnsdate";
		Query qr=session.createQuery(qrystr);
		ob=qr.list();
		session.getTransaction().commit();
		endOperations(session);
		return ob;
	}

}
